#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "qcustomplot.h"
#include "dataclass.h"
#include <QFileDialog>
#include <QDebug>


DataClass *dataC = new DataClass;
QVector<double> diffX(1000);
QVector<double> yPitch(1000), yRoll(1000);

float minX,maxX,minY,maxY;

bool flag[13];


MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    QCustomPlot *customPlot = ui->widget;//customPlot指针指向控件

    //下面两行代码设置图例可见，并设置风格
    customPlot->legend->setVisible(true);
    customPlot->legend->setFont(QFont("Helvetica",9));

    ui->actionPitch->setChecked(true);
    ui->actionRoll->setChecked(true);

    checkPitch();
    checkRoll();

    //下面代码的功能：
    //连接Wave按钮和操作：如果选择，则正常绘制数据，如果不选择，则置零该通道数据

    connect(ui->actionPitch, &QAction::triggered, this, [=](){
        checkPitch();
        findPlot();
    });
    connect(ui->actionRoll, &QAction::triggered, this, [=](){
        checkRoll();
        findPlot();
    });

    connect(ui->actionWaveform, &QAction::triggered, this, &MainWindow::waveForm);
    connect(ui->actionHome, &QAction::triggered, this, &MainWindow::homeWaveForm);

    //设置对象树
    customPlot->setParent(this);

    //下面代码的功能：
    //添加图线，并设置颜色
    customPlot->addGraph();
    customPlot->graph(0)->setPen(QPen(Qt::blue));

    customPlot->addGraph();
    customPlot->graph(1)->setPen(QPen(Qt::red));

    //下面12行代码设置图例中的graph名称
    customPlot->graph(0)->setName("pitch");
    customPlot->graph(1)->setName("roll");

    //下面4行代码设置customPlot更好看
    customPlot->xAxis2->setVisible(true);
    customPlot->xAxis2->setTickLabels(false);
    customPlot->yAxis2->setVisible(true);
    customPlot->yAxis2->setTickLabels(false);

    /*通过下面这句语句，可以屏蔽掉QCustomPlot的鼠标消息*/
    customPlot->setAttribute(Qt::WA_TransparentForMouseEvents, true);

}

void MainWindow::checkPitch(){
    if(ui->actionPitch->isChecked()){
        flag[11] = true;
    }else{
        flag[11] = false;
    }
}
void MainWindow::checkRoll(){
    if(ui->actionRoll->isChecked()){
        flag[12] = true;
    }else{
        flag[12] = false;
    }
}

void MainWindow::mD2QVector(){
    for (int i=0; i<1000; i++){
        diffX[i] = i;
        yPitch[i] = dataC->pitchCa[i];
        yRoll[i] = dataC->rollCa[i];
    }
    ui->widget->graph(0)->setData(diffX, yPitch);
    ui->widget->graph(1)->setData(diffX, yRoll);
}

void MainWindow::findPlot(){
    dataC->findMaxMinData(flag);
    mD2QVector();
    ui->widget->replot();
}

void MainWindow::homeWaveForm(){
    maxX = dataC->indexP;
    minX = 0;
    minY = -200;
    maxY = 200;
    dataC->findMaxMinData(flag);
    qDebug()<<"find max/min/ind over";

    mD2QVector();

    ui->widget->graph(0)->rescaleAxes();
    ui->widget->yAxis->setRange(minY, maxY);
    ui->widget->replot();
}

void MainWindow::waveForm(){
    QString str = QFileDialog::getOpenFileName(this,"打开文件", "C:/Users/62320/Desktop", "(*.txt)");
    qDebug()<<str;
    dataC->txt2dataV(str.toUtf8().data());
    qDebug()<<"txt to dataVector over";
    homeWaveForm();
    QLabel * label = new QLabel("文件路径："+str,this);
    ui->statusBar->addWidget(label);
}

void MainWindow::resetRange(){
    float minmin;
    if((end_x_val-begin_x_val)<2&(end_x_val-begin_x_val)>-2){
        QMessageBox::warning(this, "warning", "警告:您刚刚横向框选距离太小");
    }else{
        if((end_y_val-begin_y_val)<2&(end_y_val-begin_y_val)>-2){
            QMessageBox::warning(this, "warning", "警告:您刚刚纵向框选距离太小");
        }else{
            if(begin_x_val<end_x_val){
                minmin = begin_x_val;
            }else{
                minmin = end_x_val;
            }

            if(minmin<0){
                QMessageBox::warning(this, "warning", "警告:您刚刚框选了Y轴左侧区域");
            }else{
                if(begin_x_val<end_x_val){
                    minX = begin_x_val;
                    maxX = end_x_val;
                }else{
                    minX = end_x_val;
                    maxX = begin_x_val;
                }
                if(begin_y_val<end_y_val){
                    minY = begin_y_val;
                    maxY = end_y_val;
                }else{
                    minY = end_y_val;
                    maxY = begin_y_val;
                }

                dataC->findMaxMinData(flag);

                mD2QVector();

                ui->widget->xAxis->setRange(minX, maxX);
                ui->widget->yAxis->setRange(minY, maxY);
                ui->widget->replot();
            }
        }
    }
}

void MainWindow::resetPos(){
    //···
    float changeX,changeY;
    float minmin=minX;
    changeX = end_x_val-begin_x_val;
    changeY = end_y_val-begin_y_val;


    minmin -= changeX;

    if(minmin<0){
        //
        maxX -= minX;
        minX = 0;
        QMessageBox::warning(this, "warning", "警告：您已向右拖动到了极限");
    }else{
        minX -= changeX;
        maxX -= changeX;
    }
    minY -= changeY;
    maxY -= changeY;

    dataC->findMaxMinData(flag);

    mD2QVector();

    ui->widget->xAxis->setRange(minX, maxX);
    ui->widget->yAxis->setRange(minY, maxY);
    ui->widget->replot();
}

void MainWindow::mousePressEvent(QMouseEvent *event){
    int x_pos = event->pos().x();
    int y_pos = event->pos().y();

    // 把鼠标坐标点 转换为 QCustomPlot 内部坐标值 （pixelToCoord 函数）
    // coordToPixel 函数与之相反 是把内部坐标值 转换为外部坐标点
    begin_x_val = ui->widget->xAxis->pixelToCoord(x_pos);
    begin_y_val = ui->widget->yAxis->pixelToCoord(y_pos);
    //这里在读取y轴坐标的时候会有误差，做一个补偿
    //begin_y_val += 120;
    qDebug()<<"BWX="<<x_pos<<";BWY="<<y_pos<<"***BPX="<<begin_x_val<<";BPY="<<begin_y_val;

}

void MainWindow::mouseReleaseEvent(QMouseEvent *event){
    int x_pos = event->pos().x();
    int y_pos = event->pos().y();

    // 把鼠标坐标点 转换为 QCustomPlot 内部坐标值 （pixelToCoord 函数）
    // coordToPixel 函数与之相反 是把内部坐标值 转换为外部坐标点
    end_x_val = ui->widget->xAxis->pixelToCoord(x_pos);
    end_y_val = ui->widget->yAxis->pixelToCoord(y_pos);
    //这里在读取y轴坐标的时候会有误差，做一个补偿
    //end_y_val += 120;
    qDebug()<<"EWX="<<x_pos<<";EWY="<<y_pos<<"***EPX="<<end_x_val<<";EPY="<<end_y_val;

    if(event->button() == Qt::RightButton){
        resetRange();
    }else if (event->button() == Qt::LeftButton) {
        resetPos();
    }
}

MainWindow::~MainWindow(){
    delete ui;
}
